<template>
    <!-- 顶部导航 -->
    <van-nav-bar title="我的收货地址" left-text="返回" left-arrow @click-left="onClickLeft" />

    <!-- 列表 -->
    <van-address-list
        v-model="addrid"
        :list="AddressList"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="onSelect"
    />
</template>
<script>
import {showNotify} from 'vant'
export default {
    created() {
        this.getAddrData()
    },
    data() {
        return {
            LoginUser:this.$cookies.get('LoginAuth'),
            addrid:'2',
            AddressList:[]
        }
    },
    methods: {
        onClickLeft()
        {
            this.$router.go(-1)
        },
        onAdd()
        {
            this.$router.push('/user/address/add')
        },
        async getAddrData()
        {
            let result = await this.$api.AddressIndex({busid:this.LoginUser.id})

            if(result.code === 0)
            {
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                return false
            }

            let AddrData = []

            for(let item of result.data)
            {
                if(item.status == 1)
                {
                    this.addrid = item.id
                }

                AddrData.push({
                    id:item.id,
                    name:item.consignee,
                    tel:item.mobile,
                    address:item.region_text + ' ' + item.address,
                    isDefault:item.status == 1 ? true : false
                })
            }

            this.AddressList = AddrData
        },
        onEdit(value)
        {
            this.$router.push({path:'/user/address/edit',query:{addrid:value.id}})
        },
        async onSelect(value)
        {
            // 选择哪个地址设置默认
            let data = {
                busid:this.LoginUser.id,
                addrid:value.id
            }

            let result = await this.$api.AddressSelect(data)

            if(result.code === 0)
            {
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                return false
            }
            
            this.getAddrData()
        }
    },
}
</script>